package com.wlyuan.open.scanner.domain.carrier;

import com.wlyuan.open.scanner.constant.Constants;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author lijing
 * @description
 */
public class RelationConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = MappingInvalid.class)
    public interface Convert {

        /**
         * 转换
         *
         * @param source
         * @return
         */
        @Mappings({
                @Mapping(source = "upstream", target = "tenantId"),
                @Mapping(source = "carrierId", target = "id"),
                @Mapping(source = "supplierInvalid", target = "status"),
        })
        @BaseMapping
        Relation convert(Relation source);

        @AfterMapping
        default void after(@MappingTarget Relation target, Relation source) {
            target.setRaw(JsonUtils.withNullJson(source));
            target.setSourceKey(String.join(Constants.STRING_JOIN, String.valueOf(source.getUpstream()), String.valueOf(source.getDownstream())));
        }

        /**
         * 转换
         *
         * @param source
         * @return
         */
        List<Relation> convert(List<Relation> source);
    }

    public static List<Relation> convert(List<Relation> creation) {
        return CONVERT.convert(creation);
    }

    public static Relation convert(Relation creation) {
        return CONVERT.convert(creation);
    }

}
